package com.gw.smscheck.service.business.focuscrowd.impl;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.alibaba.fastjson.support.odps.CodecCheck;
import com.gw.smscheck.entity.business.alarm.AlarmInfo;
import com.gw.smscheck.service.business.alarm.AlarmInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gw.smscheck.dao.business.focuscrowd.FocusCrowdMapper;
import com.gw.smscheck.entity.business.focuscrowd.FocusCrowd;
import com.gw.smscheck.service.business.focuscrowd.FocusCrowdService;
@Service
public class FocusCrowdServiceImpl implements FocusCrowdService {
	@Autowired
	private FocusCrowdMapper focusCrowdMapper;
	@Autowired
	private AlarmInfoService alarmInfoService;

	/**
	 * 添加重点人员
	 * @param focusCrowd
	 * @return
	 */
	@Override
	public Integer add(FocusCrowd focusCrowd) {
		// 生成编号
		StringBuilder code = new StringBuilder();
		String dateString = new SimpleDateFormat("yyyyMMdd").format(new Date());
		code.append(dateString);
		// 获取当天最大编号
		String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		String result = focusCrowdMapper.getMaxCode(date);
		if(result == null){
			focusCrowd.setCode(code + "001");
		}else {
			String str = result.substring(result.length() - 3);
			int i = Integer.parseInt(str) + 1;// 当前编号等于最大编号+1
			String lastCode = String.valueOf(i);
			// 保留3的位数，0 代表前面补充0，num 代表长度为4，d代表参数为正数型
			code.append(String.format("%0" + 3 + "d", Integer.parseInt(lastCode)));
			focusCrowd.setCode(code.toString());
		}
		// 判断布控日期
		if(focusCrowd.getTimeType() == null){
			return null;
		}
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		switch (focusCrowd.getTimeType()){
			case 1: // 按天
				if(focusCrowd.getDays() == null){
					return null;
				}
				c.set(Calendar.HOUR_OF_DAY, 00);
				c.set(Calendar.MINUTE, 00);
				c.set(Calendar.SECOND, 00);
				focusCrowd.setTimeStart(sf.format(c.getTime()));// 起始时间
				c.add(Calendar.DAY_OF_MONTH, focusCrowd.getDays()-1);
				c.set(Calendar.HOUR_OF_DAY, 23);
				c.set(Calendar.MINUTE, 59);
				c.set(Calendar.SECOND, 59);
				focusCrowd.setTimeEnd(sf.format(c.getTime()));// 终止时间
				break;
			case 2: // 长期
				c.set(Calendar.HOUR_OF_DAY, 00);
				c.set(Calendar.MINUTE, 00);
				c.set(Calendar.SECOND, 00);
				focusCrowd.setTimeStart(sf.format(c.getTime()));// 起始时间
				focusCrowd.setTimeEnd("2099-12-01 23:59:59");// 终止时间
				break;
			case 3: // 自定义
				String startTime = focusCrowd.getTimeStart() + " 00:00:00";
				focusCrowd.setTimeStart(startTime); // 起始时间
				String endTime = focusCrowd.getTimeEnd() + " 23:59:59";
				focusCrowd.setTimeEnd(endTime);// 终止时间
				break;
		}
		Integer resultI = focusCrowdMapper.add(focusCrowd);
		if(resultI > 0){ // 添加重点人员成功，继续添加告警业务
			addAlarmInfo(focusCrowd);
		}
		return resultI;
	}

	/**
	 * 添加关注人员告警业务
	 * @param alarmInfo
	 */
	private void addAlarmInfo(FocusCrowd focusCrowd){
		// 进入指定地点告警
		if(focusCrowd.getEnterAlarmValue() !=null && focusCrowd.getEnterAlarmValueIds() !=null ){
			AlarmInfo alarmInfo = new AlarmInfo();
			alarmInfo.setFocusId(focusCrowd.getId());// 重点人员id
			alarmInfo.setFocusPh(focusCrowd.getPhoneNum()); // 重点人员手机号
			alarmInfo.setAlarmType(5);// 进入指定地点告警
			alarmInfo.setValue(StringUtils.strip(focusCrowd.getEnterAlarmValue().toString(),"[]")); // 值
			alarmInfo.setIds(StringUtils.strip(focusCrowd.getEnterAlarmValueIds().toString(),"[]"));// 值ids
			alarmInfo.setState(1);
			alarmInfoService.add(alarmInfo);
		}
		// 离开辖区告警
		if(focusCrowd.getLeaveAlarmValue() != null && focusCrowd.getEnterAlarmValueIds() != null){
			AlarmInfo alarmInfo = new AlarmInfo();
			alarmInfo.setFocusId(focusCrowd.getId());// 重点人员id
			alarmInfo.setFocusPh(focusCrowd.getPhoneNum()); // 重点人员手机号
			alarmInfo.setAlarmType(6);// 进入指定地点告警
			alarmInfo.setValue(StringUtils.strip(focusCrowd.getLeaveAlarmValue().toString(),"[]")); // 值
			alarmInfo.setIds(StringUtils.strip(focusCrowd.getLeaveAlarmValueIds().toString(),"[]"));// 值ids
			alarmInfo.setState(1);
			alarmInfoService.add(alarmInfo);
		}
	}



	@Override
	public List<FocusCrowd> findAll() {
		return focusCrowdMapper.findAll();
	}
	@Override
	public PageInfo<FocusCrowd> pageSelect(FocusCrowd dto) {
		PageHelper.startPage(dto.getPage(), dto.getRows(), "create_time desc");// 排序
		List<FocusCrowd> resultList = focusCrowdMapper.pageSelect(dto);
		PageInfo<FocusCrowd> pageInfo = new PageInfo<>(resultList);
		return pageInfo;
	}

	/**
	 * 根据id查询
	 * @param dto
	 * @return
	 */
	@Override
	public FocusCrowd findById(Integer dto) {
		if(dto == null || dto == 0){
			return null;
		}
		FocusCrowd focusCrowd = focusCrowdMapper.findById(dto);
		// 查询告警业务
		List<AlarmInfo> alarmInfoList = alarmInfoService.findByFocusPh(focusCrowd.getPhoneNum()); // 根据重点关注人手机查询
		for(AlarmInfo alarmInfo: alarmInfoList){
			if(alarmInfo.getAlarmType() == 5){// 进入区域告警
				List<String> enterValue = Arrays.asList(alarmInfo.getValue().split(","));
				focusCrowd.setEnterAlarmValue(enterValue);
				List<String> enterValueIds = Arrays.asList(alarmInfo.getIds().split(","));
				focusCrowd.setEnterAlarmValueIds(enterValueIds);
			}
			if(alarmInfo.getAlarmType() == 6){// 离开区域告警
				List<String> leaveValue = Arrays.asList(alarmInfo.getValue().split(","));
				focusCrowd.setLeaveAlarmValue(leaveValue);
				List<String> leaveValueIds = Arrays.asList(alarmInfo.getIds().split(","));
				focusCrowd.setLeaveAlarmValueIds(leaveValueIds);
			}
		}
		return focusCrowd;
	}

	/**
	 * 修改
	 * @param focusCrowd
	 * @return
	 */
	@Override
	public Integer update(FocusCrowd focusCrowd) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		switch (focusCrowd.getTimeType()){
			case 1: // 按天
				if(focusCrowd.getDays() == null){
					return null;
				}
				c.set(Calendar.HOUR_OF_DAY, 00);
				c.set(Calendar.MINUTE, 00);
				c.set(Calendar.SECOND, 00);
				focusCrowd.setTimeStart(sf.format(c.getTime()));// 起始时间
				c.add(Calendar.DAY_OF_MONTH, focusCrowd.getDays()-1);
				c.set(Calendar.HOUR_OF_DAY, 23);
				c.set(Calendar.MINUTE, 59);
				c.set(Calendar.SECOND, 59);
				focusCrowd.setTimeEnd(sf.format(c.getTime()));// 终止时间
				break;
			case 2: // 长期
				c.set(Calendar.HOUR_OF_DAY, 00);
				c.set(Calendar.MINUTE, 00);
				c.set(Calendar.SECOND, 00);
				focusCrowd.setTimeStart(sf.format(c.getTime()));// 起始时间
				focusCrowd.setTimeEnd("2099-12-01 23:59:59");// 终止时间
				break;
			case 3: // 自定义
				String startTime = focusCrowd.getTimeStart() + " 00:00:00";
				focusCrowd.setTimeStart(startTime); // 起始时间
				String endTime = focusCrowd.getTimeEnd() + " 23:59:59";
				focusCrowd.setTimeEnd(endTime);// 终止时间
				break;
		}
		Integer resultI = focusCrowdMapper.update(focusCrowd);
		if(resultI > 0){ // 重点人员更新成功
			//修改进入区域告警
			updateEnterAlarm(focusCrowd);

			//修改离开区域告警
			updateLeaveAlarm(focusCrowd);
		}
		return resultI;
	}

	/**
	 * 修改离开区域告警
	 * @param focusCrowd
	 * @return
	 */
	private Integer updateLeaveAlarm(FocusCrowd focusCrowd){
		Integer result = 0;
		// 离开辖区告警
		if(focusCrowd.getLeaveAlarmValue() != null && focusCrowd.getLeaveAlarmValueIds() != null){
			AlarmInfo alarmInfo = new AlarmInfo();
			alarmInfo.setFocusId(focusCrowd.getId());
			alarmInfo.setAlarmType(6);
			// 1.根据重点人id和类型查询告警业务是否存在
			List<AlarmInfo> alarmInfoList = alarmInfoService.findByFocusIdAndType(alarmInfo);
			// 2.存在就修改，不存在就新增
			if(alarmInfoList.size() != 0){// 修改
				AlarmInfo updateAlarmInfo = new AlarmInfo();
				updateAlarmInfo.setFocusId(focusCrowd.getId());// 重点人员id
				updateAlarmInfo.setFocusPh(focusCrowd.getPhoneNum()); // 重点人员手机号
				updateAlarmInfo.setAlarmType(6);// 进入指定地点告警
				updateAlarmInfo.setValue(StringUtils.strip(focusCrowd.getLeaveAlarmValue().toString(),"[]")); // 值
				updateAlarmInfo.setIds(StringUtils.strip(focusCrowd.getLeaveAlarmValueIds().toString(),"[]"));// 值ids
				updateAlarmInfo.setState(1);
				result = alarmInfoService.update(updateAlarmInfo);
			}else {// 添加
				AlarmInfo addAlarmInfo = new AlarmInfo();
				addAlarmInfo.setFocusId(focusCrowd.getId());// 重点人员id
				addAlarmInfo.setFocusPh(focusCrowd.getPhoneNum()); // 重点人员手机号
				addAlarmInfo.setAlarmType(6);// 进入指定地点告警
				addAlarmInfo.setValue(StringUtils.strip(focusCrowd.getLeaveAlarmValue().toString(),"[]")); // 值
				addAlarmInfo.setIds(StringUtils.strip(focusCrowd.getLeaveAlarmValueIds().toString(),"[]"));// 值ids
				addAlarmInfo.setState(1);
				result = alarmInfoService.add(addAlarmInfo);
			}
		}else {
			AlarmInfo alarmInfo = new AlarmInfo();
			alarmInfo.setFocusId(focusCrowd.getId());
			alarmInfo.setAlarmType(6);
			result = alarmInfoService.delByFocusIdAndType(alarmInfo);
		}
		return result;
	}

	/**
	 * 修改进入区域告警
	 * @return
	 */
	private Integer updateEnterAlarm(FocusCrowd focusCrowd){
		Integer result = 0;
		// 进入指定地点告警业务
		if(focusCrowd.getEnterAlarmValue() != null && focusCrowd.getEnterAlarmValueIds() != null){
			AlarmInfo alarmInfo = new AlarmInfo();
			alarmInfo.setFocusId(focusCrowd.getId());
			alarmInfo.setAlarmType(5);
			// 1.根据重点人id和类型查询告警业务是否存在
			List<AlarmInfo> alarmInfoList = alarmInfoService.findByFocusIdAndType(alarmInfo);
			// 2.存在就修改，不存在就新增
			if(alarmInfoList.size() != 0){// 修改
				AlarmInfo updateAlarmInfo = new AlarmInfo();
				updateAlarmInfo.setFocusId(focusCrowd.getId());// 重点人员id
				updateAlarmInfo.setFocusPh(focusCrowd.getPhoneNum()); // 重点人员手机号
				updateAlarmInfo.setAlarmType(5);// 进入指定地点告警
				updateAlarmInfo.setValue(StringUtils.strip(focusCrowd.getEnterAlarmValue().toString(),"[]")); // 值
				updateAlarmInfo.setIds(StringUtils.strip(focusCrowd.getEnterAlarmValueIds().toString(),"[]"));// 值ids
				updateAlarmInfo.setState(1);
				result = alarmInfoService.update(updateAlarmInfo);
			}else {// 添加
				AlarmInfo addAlarmInfo = new AlarmInfo();
				addAlarmInfo.setFocusId(focusCrowd.getId());// 重点人员id
				addAlarmInfo.setFocusPh(focusCrowd.getPhoneNum()); // 重点人员手机号
				addAlarmInfo.setAlarmType(5);// 进入指定地点告警
				addAlarmInfo.setValue(StringUtils.strip(focusCrowd.getEnterAlarmValue().toString(),"[]")); // 值
				addAlarmInfo.setIds(StringUtils.strip(focusCrowd.getEnterAlarmValueIds().toString(),"[]"));// 值ids
				addAlarmInfo.setState(1);
				result = alarmInfoService.add(addAlarmInfo);
			}
		}else {// 如果为空，说明取消告警区域
			AlarmInfo alarmInfo = new AlarmInfo();
			alarmInfo.setFocusId(focusCrowd.getId());
			alarmInfo.setAlarmType(5);
			result = alarmInfoService.delByFocusIdAndType(alarmInfo);
		}
		return result;
	}

	@Override
	public Integer delByIds(Integer id) {
		return focusCrowdMapper.delByIds(id);
	}

	/**
	 * 多编号查询
	 * @param codes
	 * @return
	 */
	@Override
	public List<FocusCrowd> findByCodes(String[] codes) {
		return focusCrowdMapper.findByCodes(codes);
	}
	/**
	 * 群体分布占比
	 * @param
	 * @return
	 */
	@Override
	public List<FocusCrowd> typeDistributed() {
		return focusCrowdMapper.typeDistributed();
	}

	/**
	 * 重点人员关注时间分布
	 * @return
	 */
	@Override
	public List<FocusCrowd> attentionTimeDistributed() {
		return focusCrowdMapper.attentionTimeDistributed();
	}

	/**
	 * 重点人数统计
	 * @param
	 * @return
	 */
	@Override
	public Integer focusCrowdCount() {
		return focusCrowdMapper.focusCrowdCount();
	}
}

